package com.qf.xiaomi.web.servlet;

import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.xiaomi.pojo.TbGoodsType;
import com.qf.xiaomi.service.goodsTypeService;
import com.qf.xiaomi.service.impl.goodsTypeServiceImpl;
import com.qf.xiaomi.utils.StringConstans;
import com.qf.xiaomi.utils.StringUtils;
import com.qf.xiaomi.web.base.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/type")
public class TypeServlet extends BaseServlet {
    goodsTypeService service = new goodsTypeServiceImpl();
    ObjectMapper objectMapper = new ObjectMapper();
    public String findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        List<TbGoodsType> goodsTypes = service.selectType();
        List<TbGoodsType> goodsTypes  = (List<TbGoodsType>) req.getServletContext().getAttribute("goodsTypeList");
        String json= objectMapper.writeValueAsString(goodsTypes);
        resp.setContentType("application/json;charset=utf-8");
        return json;
    }
//    public String add(HttpServletRequest req,HttpServletResponse resp){
//        String parentStr = req.getParameter("Parent");
//
//        long parent = Long.parseLong(parentStr);
//        String name = req.getParameter("name");
//
//        TbGoodsType tbGoodsType = new TbGoodsType();
//        tbGoodsType.setParent(parent);
//        tbGoodsType.setName(name);
//         tbGoodsType = service.addType(tbGoodsType);
//        List<TbGoodsType> goodsTypes  = (List<TbGoodsType>) req.getServletContext().getAttribute("goodsTypeList");
//        goodsTypes.add(tbGoodsType);
//        return StringConstans.REDIRECT+"/admin/showGoodsType.jsp";
//    }
    public String delete(HttpServletRequest req,HttpServletResponse resp){
        String id = req.getParameter("id");
        long TypeId = Long.parseLong(id);
        List<TbGoodsType>  tbGoodsType = new ArrayList<>();
        tbGoodsType = service.delByTypeId(tbGoodsType, TypeId);
        req.setAttribute("goodsTypeList",tbGoodsType);
        return StringConstans.FORWARD+"/admin/showGoodsType.jsp";
    }
}
